package org.example.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.model.common.AppHttpCodeEnum;
import org.example.model.common.ResponseResult;
import org.example.model.user.dtos.LoginDto;
import org.example.model.user.dtos.UserInfoNoPwdDto;
import org.example.model.user.entities.UserInfo;
import org.example.model.user.vos.AddUserInfoVo;
import org.example.model.user.vos.LoginVo;
import org.example.model.user.vos.UpdatePasswordVo;
import org.example.service.user.UserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户信息表现层
 */
@CrossOrigin
@RestController
@RequestMapping("/user-info")
@Api(value = "用户接口", tags = {"用户信息接口"})
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 添加一条用户信息
     */
    @PostMapping
    @ApiOperation("添加一条用户信息")
    public ResponseResult<Boolean> AddUserInfo(@RequestBody AddUserInfoVo addUserInfoVo) {
        return userInfoService.InsertUserInfo(addUserInfoVo);
    }

    /**
     * 根据id修改一条用户信息
     */
    @PutMapping
    @ApiOperation("根据id修改一条用户信息")
    public ResponseResult<Boolean> UpdateUserInfo(@RequestBody UserInfoNoPwdDto userInfoNoPwdDto) {
        AddUserInfoVo addUserInfoVo = new AddUserInfoVo();
        BeanUtils.copyProperties(userInfoNoPwdDto, addUserInfoVo);
        return userInfoService.UpdateUserInfoById(addUserInfoVo);
    }

    /**
     * 根据id修改用户密码
     */
    @PutMapping("/password")
    @ApiOperation("根据id修改用户密码")
    public ResponseResult<Boolean> UpdateUserPassword(@RequestBody UpdatePasswordVo updatePasswordVo) {
        AddUserInfoVo addUserInfoVo = new AddUserInfoVo();
        BeanUtils.copyProperties(updatePasswordVo, addUserInfoVo);
        return userInfoService.UpdateUserInfoById(addUserInfoVo);
    }

    /**
     * 根据id删除一条用户信息
     */
    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除一条用户信息")
    public ResponseResult<Boolean> DeleteUserInfo(@PathVariable int id) {
        return userInfoService.DeleteUserInfoById(id);
    }

    /**
     * 获取所有用户信息
     */
    @GetMapping("/user-list")
    @ApiOperation("获取所有用户信息")
    public ResponseResult<List<UserInfoNoPwdDto>> GetUserInfoList() {
        return userInfoService.SelectUserInfoByConditions();
    }


    /**
     * 获取所有用户信息
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id获取用户信息")
    public ResponseResult<UserInfoNoPwdDto> GetUserInfoById(@PathVariable int id) {
        UserInfo userInfo = userInfoService.getById(id);
        UserInfoNoPwdDto userInfoNoPwdDto = new UserInfoNoPwdDto();
        BeanUtils.copyProperties(userInfo, userInfoNoPwdDto);
        ResponseResult<UserInfoNoPwdDto> result = new ResponseResult<>();
        result.ok(AppHttpCodeEnum.SUCCESS.getCode(), userInfoNoPwdDto, AppHttpCodeEnum.SUCCESS.getErrorMessage());
        return result;
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public ResponseResult<LoginDto> Login(@RequestBody LoginVo loginVo) {
        return userInfoService.Login(loginVo);
    }

    /**
     * 用户注册
     */
    @PostMapping("/register")
    @ApiOperation("用户注册")
    public ResponseResult<LoginDto> Register(@RequestBody LoginVo loginVo) {
        return userInfoService.Register(loginVo);
    }
}
